import logging
import time

import pytest
from resource.pom.keyboard import Keyboard


class Test:
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1、进入桌面，长按应用拖动合并到文件夹中')
        device.refresh_layout()
        badges = device.get_elements_by_type('Badge')
        x0, y0 = device.center_of_element(badges[0])
        x1, y1 = device.center_of_element(badges[1])
        device.drag(x0, y0, x1, y1)
        logging.info('预期结果1、应用合并到文件夹中无异常')
        device.refresh_layout()
        device.assert_text_exist('新建文件夹1')

        logging.info('步骤2、长按文件夹重命名')
        new_folder = device.get_element_by_text('新建文件夹1')
        nx, ny = device.center_of_element(new_folder)
        device.long_click(nx, ny - 110)
        device.refresh_layout()
        rename = device.get_element_by_text('重命名')
        device.click_element(rename)
        # 删除文件夹名称,点击文件夹命名框
        device.click(547, 209)
        device.click(360, 209)
        device.click(*self.keyboard.key_a)
        device.click(*self.keyboard.close)
        # 第一次退出编辑，第二次退出文件夹
        device.click(360, 640)
        device.click(360, 640)
        logging.info('预期结果2、文件重命名显示正常')
        device.refresh_layout()
        device.assert_text_exist('a')

        logging.info('步骤3、进入文件夹中添加应用、去除应用')
        device.click(nx, ny - 110)
        device.refresh_layout()
        add = device.get_element_by_text('添加')
        ax0, ay0 = device.center_of_element(add)
        device.click(ax0, ay0 - 55)
        device.refresh_layout()
        toggle0 = device.get_element_by_type('Toggle', index=0)
        toggle1 = device.get_element_by_type('Toggle', index=1)
        toggle2 = device.get_element_by_type('Toggle', index=2)
        confirm = device.get_element_by_text('确认')
        device.click_element(toggle2)
        device.click_element(confirm)

        logging.info('预期结果3、文件夹内可正常添加应用，应用移除正常')
        device.refresh_layout()
        add = device.get_element_by_text('添加')
        ax1, ay1 = device.center_of_element(add)
        assert (ax0, ay0) != (ax1, ay1), '添加应用异常'

        device.click(ax1, ay1 - 55)
        device.click_element(toggle0)
        device.click_element(toggle1)
        device.click_element(toggle2)
        device.click_element(confirm)
        device.refresh_layout()
        device.assert_text_exist('a', expect=False)
